package com.example.soscall.utils;

import android.os.Build;
import android.util.Log;

import com.ecarx.common.Constant;
import com.example.soscall.activity.MainActivity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.CountDownLatch;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpUtils {
    private static final String TAG = Constant.Companion.getBASE_TAG() + HttpUtils.class.getSimpleName();

    public  static String getJsonStringContent(String url_path){
        try{
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{new MyTrustManager()}, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new MyHostnameVerifier());
            URL url = new URL(url_path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("User-agent",getUserAgent());
            connection.setConnectTimeout(3000);
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            connection.connect();
            int code = connection.getResponseCode();
            Log.d(TAG, "code: "+code);
            if (code==200){
                return changeInputStream(connection.getInputStream());
            }
        }catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "jsonStringContent"+e.getLocalizedMessage());
            return "";
        }finally {

        }
        return "";
    }

    private static String changeInputStream(InputStream inputStream) {
        String jsonString = "";
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int len = 0;
        byte[] data = new byte[1024];
        try {
            while ((len=inputStream.read(data))!=-1){
                outputStream.write(data,0,len);
            } jsonString = new String(outputStream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonString;
    }

    private static class MyHostnameVerifier implements HostnameVerifier {


        @Override
        public boolean verify(String hostname, SSLSession session) {
            // 信任所有host，直接返回true
            return true;
        }
    }

    private static class MyTrustManager implements X509TrustManager {


        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException
        {


        }


        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException
        {


        }


        @Override
        public X509Certificate[] getAcceptedIssuers()
        {
            return null;
        }
    }

    public static String getUserAgent() {
       String useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
                "AppleWebKit/537.36 (KHTML, like Gecko) " +
                "Chrome/52.0.2743.116 Safari/537.36";
       return useragent;

    }
}
